import type { EventHandler, EventHandlerRequest } from 'h3'

export const defineWrappedResponseHandler = <T extends EventHandlerRequest, D> (
  handler: EventHandler<T, D>
): EventHandler<T, D> =>
  defineEventHandler<T>(async event => {
    try {
        const data = await handler(event)
        return {
            data,
            code: 200,
            message: '请求成功'
        }
    } catch (err) {
        // 错误处理
        return {
            error: err,
            message: '系统异常',
            code: 500,
        }
    }
})